////////// 函数类型的强制转换示例

enum EventType { Mouse,Keyboard};
interface Event {
    timestamp : number;
}

interface MouseEvent extends Event {
    x1: number;
    y1: number;
};

interface KeyEvent extends Event {
    keyCode : number;
};

function listenEvent(eventType : EventType, handler : (n:Event)=>void) {
    // do sth ;
}

// 默认函数类型转换
listenEvent(EventType.Mouse , (e:MouseEvent)=>console.log(e.x1+','+e.y1));

// 强制函数类型转换
listenEvent(EventType.Keyboard , <(e:Event)=>void> ((e:KeyEvent)=>{console.log(e.keyCode);}));



///////////// 类型保护示例
interface Fish { swim : Function;}
interface Bird { fly : Function; }

function isFish(pet: Fish | Bird): pet is Fish {
    return (<Fish>pet).swim !== undefined;
}


/// 函数调用时指定类型
let cacheKey = 'en';
// require(`element-ui/lib/locale/lang/${cacheKey}`).default



/// 接口声明函数
interface SearchFunc {
    (source: string, subString: string): boolean;
}
let mySearch2: SearchFunc;
mySearch = function(source: string, subString: string) {
  let result = source.search(subString);
  return result > -1;
}

/// 声明可索引的类型
interface StringArray {
    [index: number]: string;
 }
 let myArray2: StringArray;
 myArray = ["Bob", "Fred"];


 /// 声明this类型
 interface Deck {
    suits: string[];
    cards: number[];
    createCardPicker(this: Deck): () => Card;
}
let createCardPicker2 = function(this: Deck) {
    return () => {
        let pickedCard = Math.floor(Math.random() * 52);
        let pickedSuit = Math.floor(pickedCard / 13);
        // 可以获得this.suites 和 this.cards
        return {suit: this.suits[pickedSuit], card: pickedCard % 13};
    };
}


/// 字符串字面量类型
type Easing = "ease-in" | "ease-out" | "ease-in-out";
function EAS(easing : Easing) {
    if(easing=="ease-in") {
        //...
    }
}